Skip to content

Filter out GIT_WORK_TREE from cloner environment variables#4549

Open
jasondamour wants to merge 3 commits into
bufbuild:mainfrom
jasondamour:worktree-fix-git-work-tree-env
Open

Filter out GIT_WORK_TREE from cloner environment variables#4549
jasondamour wants to merge 3 commits into
bufbuild:mainfrom
jasondamour:worktree-fix-git-work-tree-env

Conversation

@jasondamour
Copy link
Copy Markdown

@jasondamour jasondamour commented May 18, 2026

Summary

Follows the same pattern as #3760 (GIT_DIR) and #3813 (GIT_INDEX_FILE) to fix the same class of issue (#3752) for git worktrees.

When buf breaking --against is run inside a git worktree (e.g. via a pre-commit hook in a Cursor/VS Code worktree), the shell sets GIT_WORK_TREE pointing at the worktree's working directory. Passing this variable into the cloner's git init / git fetch / git checkout sequence causes git to reject the operation:

fatal: GIT_WORK_TREE (or --work-tree=<directory>) not allowed without specifying GIT_DIR (or --git-dir=<directory>).

The fix is a one-liner: add "GIT_WORK_TREE": "" to the existing env-sanitization map in private/pkg/git/cloner.go, so the cloner always starts with a clean git environment regardless of how buf was invoked.

Test plan

  • Run buf breaking --against from inside a git worktree (e.g. .cursor/worktrees/...) and confirm no GIT_WORK_TREE error
  • Existing unit/integration tests pass (go test ./private/pkg/git/...)

🤖 Generated with Claude Code

When buf breaking --against is run inside a git worktree, the
GIT_WORK_TREE environment variable is set by the shell. Passing it
to the cloner's git init/fetch/checkout sequence causes git to error:

  fatal: GIT_WORK_TREE (or --work-tree=<directory>) not allowed
  without specifying GIT_DIR (or --git-dir=<directory>).

Follows the same pattern as bufbuild#3760 (GIT_DIR) and bufbuild#3813 (GIT_INDEX_FILE).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 18, 2026

CLA assistant check
All committers have signed the CLA.

@doriable
Copy link
Copy Markdown
Member

This generally looks good -- any chance you could merge main to pull in the test cases from #4550 and implement a similar test case for GIT_WORK_TREE?

jasondamour and others added 2 commits May 19, 2026 14:33
Covers the worktree scenario where GIT_WORK_TREE is set in the
environment, matching the env override tests added in bufbuild#4550.

Co-authored-by: Cursor <cursoragent@cursor.com>
@jasondamour
Copy link
Copy Markdown
Author

@doriable done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants